package com.booking.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.booking.bean.Seller;
import com.booking.bean.Shop;
import com.booking.dao.SellerDAO;

@Repository
public class SellerDAOImpl implements SellerDAO {

	@Autowired
	private SessionFactory sessionFactory;
	//获取和当前线程绑定的 Session. 
	public Session getSession(){
		return sessionFactory.getCurrentSession();
	}
	
	@Transactional
	@Override
	public int findCount() {
		String hql="select count(*) from Seller";
		
		org.hibernate.Query query = this.getSession().createQuery(hql); 
		
		List list = query.list();
		if(list.size()>0)
			return  Integer.parseInt(String.valueOf(list.get(0))) ;
		
		return 0;
	}
	
	@Transactional
	@Override
	public List<Seller> findByPage(int offset, int length) {
	        List<Seller> entitylist=null;
	        try{
	            Query query = this.getSession().createQuery("from Seller ");
	            query.setFirstResult(offset);
	            query.setMaxResults(length);
	            entitylist = query.list();
	            
	        }catch(RuntimeException re){
	            throw re;
	        }
	        
	        return entitylist;
	    }

	
	@Transactional
	@Override
	public Seller querySeller(int id) {
		
			String hql="select s  from  Seller s where s.id = ? ";
			org.hibernate.Query query = this.getSession().createQuery(hql); 
			
			query.setLong(0,id);
			List<Seller> list = query.list();
			
			
			return list.get(0);
		}
	
	@Transactional
	@Override
	public List<Seller> querySeller() {
		String hql="select s  from  Seller s order by id asc ";
		org.hibernate.Query query = this.getSession().createQuery(hql); 
		
		List<Seller> list = query.list();
		
		
		return list;
	}
	

}
